package cs2340.lostmystuff;

import java.io.OutputStreamWriter;
import java.util.LinkedList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ItemAddActivity extends Activity {

	private EditText itName;
	private EditText itDes;
	//private final ItemTable iTable = new ItemTable();
	public String itemName, itemDes;
	private final LinkedList<Item> itemList = new LinkedList<Item>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_add);
		// Show the Up button in the action bar.
		setupActionBar();
		
		 Button sub = (Button) findViewById(R.id.submitItem);
	        
	        sub.setOnClickListener(new View.OnClickListener() {
	        	public void onClick(View v){
	        		itName = (EditText) findViewById(R.id.itName2);
	        		itDes = (EditText) findViewById(R.id.editText2);
	        		itemName = itName.getText().toString();
	        		itemDes = itDes.getText().toString();
	        		
	        		Item temp = new Item(itemName, itemDes);
	        		itemList.add(temp);
	        		try {


	                     OutputStreamWriter o = new OutputStreamWriter(openFileOutput("foundItemList.txt",MODE_APPEND));
	                     
	                      o.write(itemName);
	                      o.write('\n');   
	                   
	                      o.close();
	                   } 

	        		 catch (java.io.IOException e) {                 

	                   }
	        		finish();
	        	}	
	        });
		
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.item_add, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void storeItem(View view) {
		
		itemName = itName.getText().toString();
		itemDes = itDes.getText().toString();
		
		Item temp = new Item(itemName, itemDes);
		itemList.add(temp);
	}
	
	public void clearField(View view) {
		Intent cIntent = getIntent();
		finish();
		startActivity(cIntent);
	}
	
}
